"""
__author__ = 'berlinyoung'
@File   :app.py
@Date   :2022/5/18 20:51
-*- coding: utf-8 -*-
"""
import os
import time

from appium import webdriver
from appium.webdriver.webdriver import WebDriver

from weixin_mobile.step1_po.page.base import Base
from weixin_mobile.step1_po.page.main_page import MainPage


class App(Base):
    # 方法：启动App
    def start_app(self):

        udid = os.getenv("udid")
        port = os.getenv("port")

        if self.driver is None:
            self.logger.info(f"driver is {self.driver}")
            # 创建json串
            caps = {}
            caps["platformName"] = "android"
            caps["deviceName"] = "HONOR V20"
            caps["appPackage"] = "com.tencent.wework"
            caps["appActivity"] = ".launch.LaunchSplashActivity"
            # caps["automationName"] = "uiautomator2"
            caps["noReset"] = True
            caps["noSign"] = True
            caps["ensureWebviewsHavePages"] = True
            caps["newCommandTimeout"] = 600
            caps["udid"] = f"{udid}"
            # 提示 自动化测试启动速度的，注意：新设备 不要添加这两个设置
            # caps['skipServerInstallation'] = True
            # caps['skipDeviceInitialization'] = True
            # 最重要的一行，完成了客户端与服务端的链接，创建driver
            self.driver = webdriver.Remote(f"http://127.0.0.1:{port}/wd/hub", caps)
            # 隐式等待，每次在调用find_element / find_elements时激活。在5秒内动态执行下一步
            self.driver.implicitly_wait(5)
        else:
            self.logger.info(f"driver is {self.driver}")
            # 复用driver，launch_app() 启动页面
            self.driver.launch_app()
        return self

    # 方法：重启
    def restart(self):
        pass

    # 方法：销毁对象driver
    def stop(self):
        self.driver.quit()

    def back(self,num=3):

        self.logger.info('back')
        for i in range(0,num):
            self.driver.back()

    # 进入app首页入口
    def goto_main(self):
        return MainPage(self.driver)

